package com.chen.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.dto.TeacherDTO;
import com.chen.dto.TeacherLoginDTO;
import com.chen.entity.Teacher;
import com.chen.mapper.ClassesMapper;
import com.chen.mapper.TeacherMapper;
import com.chen.service.TeacherService;
import com.chen.vo.TeacherVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 教师相关服务实现
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    // 班级
    @Autowired
    private ClassesMapper classesMapper;

    /**
     * 教师登陆
     * @param dto
     * @return
     */
    @Override
    public TeacherVO teacherLogin(TeacherLoginDTO dto) {
        String account = dto.getTeacherAccount();
        Assert.isTrue(ObjectUtil.isNotEmpty(account), "教师登陆账号不能为空");
        String pwd = dto.getTeacherPwd();
        Assert.isTrue(ObjectUtil.isNotEmpty(pwd), "教师登陆密码不能为空");

        // 执行数据库查询
        Teacher teacher = baseMapper.findTeacherByAccountAndPwd(account, pwd);
        if (ObjectUtil.isNotEmpty(teacher)) {
            return BeanUtil.toBean(teacher, TeacherVO.class);
        }
        return null;
    }

    /**
     * 查找所有非班主任教师
     * @return
     */
    @Override
    public List<TeacherVO> findTeacherNoHead() {
        List<Teacher> teachers = baseMapper.findTeachersByIsWorkZero();
        if (ObjectUtil.isNotEmpty(teachers)) {
            return BeanUtil.copyToList(teachers, TeacherVO.class);
        }
        return Collections.emptyList();
    }

    @Override
    public Long findAllCount() {
        return baseMapper.findAllCount();
    }

    @Override
    public List<TeacherVO> listPage(Integer startIndex, Integer pageShow) {
        return baseMapper.listPage(startIndex, pageShow);
    }

    @Override
    public Integer saveTeacher(TeacherDTO dto) {
        Teacher teacher = BeanUtil.toBean(dto, Teacher.class);
        if (ObjectUtil.isEmpty(teacher.getIsWork())) {
            teacher.setIsWork(0);
        }
        return baseMapper.insert(teacher);
    }

    /**
     * 修改教师信息
     * @param dto
     * @return
     */
    @Override
    public Integer updateTeacher(TeacherDTO dto) {
        Teacher teacher = BeanUtil.toBean(dto, Teacher.class);
        if (ObjectUtil.isEmpty(teacher.getIsWork())) {
            teacher.setIsWork(0);
        }
        return baseMapper.updateById(teacher);
    }

    @Override
    public Integer deleteTeacher(Integer teacherId) {
        Long countByTeacherId = classesMapper.findCountByTeacherId(teacherId);
        if (countByTeacherId == 0) {
            baseMapper.deleteById(teacherId);
        }
        return 0;
    }

    /**
     * 根据id获取教师信息
     * @param id
     * @return
     */
    @Override
    public TeacherVO getTeacherById(Integer id) {
        return baseMapper.getTeacherById(id);
    }
}
